using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Resources;
using LeisureCenter.LibraryBuilderThreads;

namespace LeisureCenter.Core
{
    public class LibraryManager : IDisposable
    {
        public PathsBrowserThread pathsBrowserThread;
        public FilesCheckerThread filesCheckerThread;
        public PlayingsLoggerThread playingsLoggerThread;
        private Dictionary<string, FileSystemWatcher> watches = new Dictionary<string, FileSystemWatcher>();

        public LibraryManager()
        {
            this.filesCheckerThread = new FilesCheckerThread();
            this.pathsBrowserThread = new PathsBrowserThread(this.filesCheckerThread);
            this.playingsLoggerThread = new PlayingsLoggerThread();
        }

        public void Initialize()
        {
            if (Environment.CommandLine.Contains(@"--no-browsing"))
                return;

            IEnumerable<string> paths = DatabaseManager.ExecuteStringsQuery(@"SELECT name FROM Paths");
            foreach (string path in paths)
            {
                AddPath(path);
            }
            pathsBrowserThread.AddPaths(paths);
            pathsBrowserThread.Wakeup();
            filesCheckerThread.Wakeup();
        }

        private void AddPath(string path)
        {
            if (!this.watches.ContainsKey(path) && Directory.Exists(path))
            {
                FileSystemWatcher watcher = new FileSystemWatcher(path);
                watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName;
                watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
                watcher.Created += new FileSystemEventHandler(watcher_CreatedOrDeleted);
                watcher.Deleted += new FileSystemEventHandler(watcher_CreatedOrDeleted);
                this.watches.Add(path, watcher);
            }
        }

        void watcher_CreatedOrDeleted(object sender, FileSystemEventArgs e)
        {
            pathsBrowserThread.AddPaths(new string[] { Path.GetDirectoryName(e.FullPath) });
            pathsBrowserThread.Wakeup();
        }

        void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            pathsBrowserThread.AddPaths(new string[] { Path.GetDirectoryName(e.OldFullPath), Path.GetDirectoryName(e.FullPath) });
            pathsBrowserThread.Wakeup();
        }

        public void AddPath(string path, long libraryId)
        {
            DatabaseManager.ExecuteNonQuery(string.Format("INSERT OR REPLACE INTO Paths (name, libraryId) VALUES ('{0}', {1})", path, libraryId));
            AddPath(path);
        }

        public void RemovePath(string path)
        {
            DatabaseManager.ExecuteNonQuery(string.Format("DELETE FROM Paths WHERE name = '{0}'", path));
        }

        #region IDisposable Membres

        public void Dispose()
        {
            pathsBrowserThread.Terminate();
            filesCheckerThread.Terminate();
        }

        #endregion
    }
}
